package com.wgts.wgts.mvp.view.activity.shopCar;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.mvp.presenter.IShoppingCarPersenter;
import com.wgts.wgts.mvp.view.activity.shopkeeper.AdvancedTaskActivity;
import com.wgts.wgts.mvp.view.activity.shopkeeper.UpgradeShopKeeperActivity;
import com.wgts.wgts.netmodle.resp.ShopCarListEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.MoHuGlide;
import com.wgts.wgts.tools.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/9/2014:58
 *
 */public class ShopCarParentRecycleAdapter extends RecyclerView.Adapter<ShopCarParentRecycleAdapter.ViewHold> {
    private Context context;
    private List<ShopCarListEntity> list;
    private IShoppingCarPersenter persenter;
    private ShoppingCarRecycleAdapter adapter;
    private FragmentManager fragmentManager;
    private upLevelClickListen upLevelClickListen;

    public List<ViewHold> myViewHolderList = new ArrayList<>();

    public ShopCarParentRecycleAdapter(Context context, List<ShopCarListEntity> list, IShoppingCarPersenter persenter,
    FragmentManager fragmentManager) {
        this.context = context;
        this.list = list;
        this.persenter = persenter;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.shop_car_parent_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold viewHold, final int i) {
        if (AppInfor.getInstance().getUserState() > 3){
            viewHold.upLevelLy.setVisibility(View.GONE);
            viewHold.shopCarUpLevel.setClickable(false);
        }else if (AppInfor.getInstance().getUserState() < 2){
            viewHold.upLevelLy.setVisibility(View.VISIBLE);
            viewHold.shopCarUpLevel.setClickable(true);
            viewHold.upLevelText.setText("去升级");
        }else {
            viewHold.upLevelLy.setVisibility(View.VISIBLE);
            viewHold.shopCarUpLevel.setClickable(true);
            viewHold.upLevelText.setText("去升级");
        }
        if (list.get(i).moneyDesc != null && !list.get(i).moneyDesc.equals("")){
            viewHold.shopCarUpLevel.setVisibility(View.VISIBLE);
            viewHold.shopStorePlaneText.setText(list.get(i).moneyDesc);
        }else {
            viewHold.shopCarUpLevel.setVisibility(View.GONE);
        }
        if (viewHold.itemView.getTag() != null && viewHold.itemView.getTag().equals("first")){
            Tools.setViewMargin(viewHold.itemView, true, 0, 0, 0, 0);
        }
        if (i == 0){
            viewHold.itemView.setTag("first");
            Tools.setViewMargin(viewHold.itemView, true, 0, 0, 5, 0);
        }
        if (list.get(i).isCheck){
            viewHold.storeShopClickImage.setSelected(true);
        }else {
            viewHold.storeShopClickImage.setSelected(false);
        }

        MoHuGlide.getInstance().glideImgsFormList(viewHold.shopCarStoreImage, list.get(i).sellerIcon, context);
        viewHold.shopCarStoreName.setText(list.get(i).sellerName);
        //店铺选中商品点击事件
        viewHold.storeShopClickLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(i).isCheck){
                    persenter.ParentCheck(i, false);
                }else {
                    persenter.ParentCheck(i, true);
                }
                persenter.totalPrice();
                persenter.chooseNumber();
                persenter.isAllCheck();
            }
        });
        //进入店铺按钮
        viewHold.watchStoreLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        //去升级按钮
        viewHold.shopCarUpLevel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (upLevelClickListen != null){
                    upLevelClickListen.upLevelClick(v, i);
                }
            }
        });
        //店铺子recycleView adapter
        CustomLineaLayoutManager customLineaLayoutManager = new CustomLineaLayoutManager(context);
        customLineaLayoutManager.setScrollEnabled(false);
        viewHold.shoppingCarRecycle.setLayoutManager(customLineaLayoutManager);
        adapter = new ShoppingCarRecycleAdapter(context, list.get(i).products, persenter, fragmentManager);
        viewHold.shoppingCarRecycle.setAdapter(adapter);

        //holder绑定对应的position
        viewHold.setDataPosition(i);
        //判断list是否还有该holder， 没有就增加
        //因为list已经持有holder的引用，所有数据自动会改变
        if (!myViewHolderList.contains(viewHold)){
            myViewHolderList.add(viewHold);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.store_shop_click_image)
        ImageView storeShopClickImage;
        @BindView(R.id.store_shop_click_ly)
        LinearLayout storeShopClickLy;
        @BindView(R.id.shop_car_store_image)
        ImageView shopCarStoreImage;
        @BindView(R.id.shop_car_store_name)
        TextView shopCarStoreName;
        @BindView(R.id.watch_store_ly)
        LinearLayout watchStoreLy;
        @BindView(R.id.shop_store_plane_text)
        TextView shopStorePlaneText;
        @BindView(R.id.up_level_ly)
        LinearLayout upLevelLy;
        @BindView(R.id.shop_car_up_level)
        LinearLayout shopCarUpLevel;
        @BindView(R.id.shopping_car_recycle)
        RecyclerView shoppingCarRecycle;
        @BindView(R.id.up_level_text)
        TextView upLevelText;

        public View itemView;
        private int position;

        private void setDataPosition(int position){
            this.position = position;
        }

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public void checkNotify(){
        for (int i = 0; i < myViewHolderList.size(); i++){
            if (list.get(i).isCheck){
                myViewHolderList.get(i).storeShopClickImage.setSelected(true);
            }else {
                myViewHolderList.get(i).storeShopClickImage.setSelected(false);
            }
            myViewHolderList.get(i).shoppingCarRecycle.getAdapter().notifyDataSetChanged();
        }
    }

    public void numberNotify(int position){
        myViewHolderList.get(position).shoppingCarRecycle.getAdapter().notifyDataSetChanged();
    }

    public void shopCheck(int position){
        for (int i = 0; i < myViewHolderList.size(); i++){
            if (list.get(i).isCheck){
                myViewHolderList.get(i).storeShopClickImage.setSelected(true);
            }else {
                myViewHolderList.get(i).storeShopClickImage.setSelected(false);
            }
        }
        myViewHolderList.get(position).shoppingCarRecycle.getAdapter().notifyDataSetChanged();
    }

    public interface upLevelClickListen{
        void upLevelClick(View view, int position);
    }
    public void setUpLevelClickListen(upLevelClickListen upLevelClickListen){
        this.upLevelClickListen = upLevelClickListen;
    }
}
